package com.yys.gulimall.product.service.impl;

import com.yys.gulimall.product.entity.AttrEntity;
import com.yys.gulimall.product.service.AttrService;
import com.yys.gulimall.product.vo.AttrGroupWIthAttrsVo;
import com.yys.gulimall.product.vo.sku.skuItem.SkuItemVo;
import com.yys.gulimall.product.vo.sku.skuItem.SpuItemGroupAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yys.common.utils.PageUtils;
import com.yys.common.utils.Query;

import com.yys.gulimall.product.dao.AttrGroupDao;
import com.yys.gulimall.product.entity.AttrGroupEntity;
import com.yys.gulimall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrservice;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {

        // key 检索条件,模糊匹配可以匹配group_id和attr_group_name
        String key = (String) params.get("key");

        // select * from pma_attr_group where catelog_id = ? and (group_id = key or attr_group_name like %key%)
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)) {
            wrapper.and(o -> o.eq("attr_group_id",key)
                    .or()
                    // likeLeft === %xxx
                    .like("attr_group_name",key));
        }

        // 查询所有
        if(catId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id", catId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    /**
     *  根据分类查出所有分组的所有属性
     * */
    @Override
    public List<AttrGroupWIthAttrsVo> getAttrGroupWithAttrs(Long catelogId) {
        // 查询分组信息
        List<AttrGroupEntity> groupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>()
                        .eq("catelog_id", catelogId)
        );

        return groupEntities.stream().map(group -> {
            AttrGroupWIthAttrsVo attrGroupWIthAttrsVo = new AttrGroupWIthAttrsVo();
            BeanUtils.copyProperties(group, attrGroupWIthAttrsVo);

            // 查询所有属性
            List<AttrEntity> relationAttr = attrservice.getRelationAttr(attrGroupWIthAttrsVo.getAttrGroupId());
            attrGroupWIthAttrsVo.setAttrs(relationAttr);

            return attrGroupWIthAttrsVo;
        }).collect(Collectors.toList());

    }

    @Override
    public List<SpuItemGroupAttrVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 查出当前spu对应的所有属性的分组信息和一级当前分组下的所有属性对应的值
        return this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
    }

}